package com.indigon.parkStory.model.objects
{
	import com.indigon.parkStory.model.ModelMain;

	public class ScoreModel extends ObjectModel
	{
		private static const GOAL_BASE_SCORE : uint = 100;
		private static const MAX_COMBO	     : uint = 12;
		private static const TIME_BONUS	     : uint = 1000;
		private static const COMBO_BONUS	 : uint = 100;
		private static const STAR_MULTI      : uint = 2;

		public var timer : uint;
		public var score : uint;
		public var combo : uint = 1;
		
		public var maxCombo : uint = 1;
		
		public var collectedGoals : uint;
		public var collectedStars : uint;
		
		public function ScoreModel(model:ModelMain)
		{
			super(model);
		}
		
		public function reset():void
		{
			timer			 = 0;
			maxCombo         = 1;
			combo 			 = 1;
			score			 = 0;
			collectedGoals	 = 0;
			collectedStars 	 = 0;
		}
		
		public override function update():void
		{
			timer++;
		}
		
		public function collectGoal():void
		{
			collectedGoals++;
			score += GOAL_BASE_SCORE * combo;
			addCombo();
			_model.frog.increaseRange();
		}
		
		public function collectStar():void
		{
			collectedStars++;
			score *= STAR_MULTI;
			addCombo();
		}
		
		public function addCombo():void
		{
			if (combo < MAX_COMBO)
			{
				combo++;
			}
			maxCombo = Math.max(maxCombo, combo);
		}
		
		public function resetCombo():void
		{
			combo = 1;
		}
		
		public function get timeScore():uint
		{
			return uint(Math.max(_model.data.maxTime - timeInSeconds, 0) * TIME_BONUS);
		}
		
		public function get comboScore():uint
		{
			return maxCombo * maxCombo * COMBO_BONUS;
		}
		
		public function get finalScore():uint
		{
			return score + timeScore + comboScore;
		}
		
		public function get finalRank():uint
		{
			var rank : uint = 1;
			
			if (willGetTimeBonus())
			{
				rank++;
			}
			
			if (willGetComboBonus())
			{
				rank++;
			}
			
			return rank;
		}
		
		public function willGetComboBonus():Boolean
		{
			return maxCombo >= _model.data.comboBonus;
		}
		
		public function willGetTimeBonus():Boolean
		{
			return timeInSeconds <= _model.data.timeBonus;
		}
		
		private function get timeInSeconds():Number
		{
			return uint((timer / _model.framerate) * 10) / 10;
		}
	}
}